<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\RepairHuanxinLists;
use app\adminapi\lists\RepairLists;
use app\adminapi\logic\RepairLogic;
use app\adminapi\validate\RepairValidate;
use app\common\model\InsurancePolicy;
use app\common\model\Repair;
use app\common\service\pay\WeChatPayService;


/**
 * Repair控制器
 * Class RepairController
 * @package app\adminapi\controller
 */
class RepairController extends BaseAdminController
{


    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public function lists()
    {
        return $this->dataLists(new RepairLists());
    }
    public function huanxinList()
    {
        return $this->dataLists(new RepairHuanxinLists());
    }

    /**
     * @notes 添加
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public function add()
    {
        $params = (new RepairValidate())->post()->goCheck('add');
        $result = RepairLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(RepairLogic::getError());
    }


    /**
     * @notes 编辑
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public function edit()
    {
        $params = (new RepairValidate())->post()->goCheck('edit');
        $result = RepairLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(RepairLogic::getError());
    }


    /**
     * @notes 删除
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public function delete()
    {
        $params = (new RepairValidate())->post()->goCheck('delete');
        RepairLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public function detail()
    {
        $params = (new RepairValidate())->goCheck('detail');
        $result = RepairLogic::detail($params);
        return $this->data($result);
    }


    public function finish()
    {
        $params = (new RepairValidate())->post()->goCheck('finish');
        $result = Repair::findOrFail($params['id']);
        $result = RepairLogic::finishRepair($result,$params['receive_express_no']);
        return $this->success('完成', [], 1, 1);

    }


    public function refund()
    {
        $params = (new RepairValidate())->post()->goCheck('refund');
        $result = Repair::findOrFail($params['id']);
        $result->status = Repair::STATUS_REFUND;
        $refundNo = generate_sn(Repair::class,'refund_no');
        $result->refund_no = $refundNo;

        $result->check_remark = $params['check_remark'];
        $result->check_img  = json_encode( $params['check_img']);


//        $refundRes = (new WeChatPayService(2))->refund([
//            'transaction_id' => $result->transaction_id,
//            'refund_sn'      => $refundNo,
//            'refund_amount'  => $result->order_amount,// 退款金额
//            'total_amount'   => $result->order_amount,// 订单金额
//        ]);
//        if ($refundRes['status'] === 'ABNORMAL') {
//            return $this->fail('微信退款失败');
//        }
        $insurance =  $result->insurance()->findOrFail();

        $insurance->status = InsurancePolicy::StATUS_FINISH ;
        $insurance->save();
        $result->save();
        return $this->success('完成', [], 1, 1);
    }






}